<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<div class="module-filter">
					<label style="margin-right: 10px">{{ $t('brand_data.startDate') }}</label>
					<el-date-picker v-model="params.date" type="monthrange" :placeholder="$t('brand_data.startDateLabel')" range-separator="to" value-format="YYYY-MM"></el-date-picker>
				</div>
				<!-- <div class="module-filter">
					<label style="margin-right: 10px">{{ $t('brand_data.endDate') }}</label>
					<el-date-picker v-model="params.endDate" type="monthrange" value-format="YYYY-MM"
						:placeholder="$t('brand_data.endDateLabel')" :disabled-date="backOptions" unlink-panels></el-date-picker>
				</div> -->
				<div class="module-filter btns">
					<el-button type="primary" icon="el-icon-search" @click="upsearch">{{ $t('table_data.searchText') }}</el-button>
					<el-button @click="reset">{{ $t('table_data.resetText') }}</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<div class="top-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">{{ $t('table_data.addText') }}</el-button>
			</div>
			<scTable ref="table" :apiObj="apiObj" row-key="id" stripe border :params="sendParams" :hideSetting="true"
				@sortChange="sortChange" :sortType="direction" width="100%">
				<!-- <el-table-column type="selection" width="50"></el-table-column> -->
				<!-- <el-table-column :label="$t('artwork_data.artworkId')" prop="artworkId" width="auto"></el-table-column> -->
				<el-table-column :label="$t('report_data.year')" prop="year" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('report_data.month')" prop="month" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('report_data.quantity')" prop="itemSalesQuantity" width="auto"></el-table-column>
				<el-table-column :label="$t('report_data.amount')" prop="itemSalesAmount" width="auto"></el-table-column>
				<el-table-column :label="$t('report_data.commission')" prop="itemSalesCommission" width="auto"></el-table-column>
				<el-table-column :label="$t('report_data.gmo')" prop="gmoFee" width="auto"></el-table-column>
				<el-table-column :label="$t('report_data.date')" prop="reportGenerationDate" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('table_data.operate')" width="auto">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">{{ $t('report_data.summary') }}</el-button>
							<el-button text type="primary" size="small" @click="table_detail(scope.row, scope.$index)">{{ $t('table_data.detail') }}</el-button>
							<!-- <el-button text type="danger" size="small" @click="table_del(scope.row, scope.$index)">{{ $t('table_data.del') }}</el-button> -->
							<!-- <el-popconfirm :title="$t('table_data.del_text')" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">{{ $t('table_data.del') }}</el-button>
								</template>
							</el-popconfirm> -->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './client/save'

export default {
	name: "client",
	components: {
		saveDialog
	},
	data() {
		return {
			direction: 'direction',
			dialog: {
				save: false
			},
			search: {
				keyword: ""
			},
			apiObj: {},
			selection: [],
			searchKey: null,
			params: {
				// currency: null,
				// movieId: null,
				// category: null,
				date: null,
				// endDate: null,
				// language: null
			},
			sendParams: {

			}
		}
	},
	async created() {
		this.apiObj = this.$API.brand_report.getReportList;
		
	},
	methods: {
		closeTag(){
			const tagList = this.$store.state.viewTags.viewTags;
			const tag = this.$route;
			const nowTagIndex = tagList.findIndex(item => item.fullPath == tag.fullPath)
			this.$store.commit("removeViewTags", tag)
			this.$store.commit("removeIframeList", tag)
			this.$store.commit("removeKeepLive", tag.name)

			const leftView = tagList[nowTagIndex - 1]
			if (leftView) {
				this.$router.push(leftView)
			} else {
				this.$router.push('/')
			}
		},
		reset() {
			// this.params.currency = null;
			// this.params.movieId = null;
			// this.params.category = null;
			this.params.date = null;
			// this.params.endDate = null;
			// this.params.language = null;
			this.upsearch();
			// this.$refs.table.upData(this.params);
		},
		upsearch() {
			this.sendParams.startDate = this.params.date[0]
			this.sendParams.endDate = this.params.date[1]
			console.log(111111,this.sendParams)
			// Object.assign(this.sendParams,this.params);
			// if(this.sendParams.startDate)this.sendParams.startDate = this.$TOOL.getLocalTime(9,this.params.startDate,true)//this.$TOOL.dateFormat(this.sendParams.startDate);
			// if(this.sendParams.endDate)this.sendParams.endDate = this.$TOOL.getLocalTime(9,this.params.endDate,true)//this.$TOOL.dateFormat(this.sendParams.endDate);
			this.$refs.table.upData(this.sendParams);
		},
		//增加
		add() {
			this.$router.push({path:'/reportTable/client/save',query:{type:'add','id':'add'}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open()
			// })
		},
		//编辑
		table_edit(row){
			this.$router.push({path:'/reportTable/client/save',query:{type:'sum','date':row.reportGenerationDate}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open('edit').setData(row)
			// })
		},
		// 详情
		table_detail(row){
			this.$router.push({path:'/reportTable/client/save',query:{type:'detail','date':row.reportGenerationDate}})
			// this.dialog.save = true
			// this.$nextTick(() => {
			// 	this.$refs.saveDialog.open('edit').setData(row)
			// })
		},
		//删除
		async table_del(row, index) {
			console.log(index)
			this.$confirm(this.$t('table_data.deltips1'), this.$t('table_data.tips'), {
				confirmButtonText: this.$t('table_data.editText'),
				cancelButtonText: this.$t('table_data.cancelText'),
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.brand.delBrand.delete(row.brandId);
				if(res.code == 'OK'){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.upData(this.params);
					this.$message.success(this.$t('table_data.delsucc'))
				}else{
					this.$alert(res.message, this.$t('table_data.tips'), {type: 'error'})
				}
			}).catch(() => {
				this.$message({
					type: 'info',
					message: 'cancel'
				});          
			});
		},
		//批量删除
		async batch_del() {
			this.$confirm(`${this.$t('table_data.deltips1')} ${this.selection.length} ${this.$t('table_data.deltips2')}`, this.$t('table_data.tips'), {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				this.selection.forEach(item => {
					this.$refs.table.tableData.forEach((itemI, indexI) => {
						if (item.id === itemI.id) {
							this.$refs.table.tableData.splice(indexI, 1)
						}
					})
				})
				loading.close();
				this.$message.success(this.$t('table_data.operasucc'))
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				data.id = new Date().getTime()
				this.$refs.table.tableData.unshift(data)
			} else if (mode == 'edit') {
				this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
					Object.assign(item, data)
				})
			}
		}
	}
}
</script>

<style scoped>
.module-filter {
	margin-right: 10px;
}

.el-header {
	--el-header-height: auto;
}

.left-panel {
	flex-wrap: wrap;
	line-height: 50px;
}

.top-panel {
	margin: 10px;
}

.scTable {
	height: calc(100% - 60px) !important;
}

.el-main.nopadding {
	margin: 0 10px;
}

.el-header {
	margin: 10px;
	border-radius: 4px;
}

@media only screen and (max-width: 820px) and (min-width: 768px) {
	.module-filter {
    flex: 0 0 30%;
    display: flex;
    flex-direction: column;
    margin: 0 10px;
	}
	.btns {
    margin: 10px;
    width: 340px;
    flex-direction: row;
	}
}
</style>
